home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / traits.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  13.9 KB  |  542 lines

  1. #ifndef __RW_TRAITS
  2. #define __RW_TRAITS
  3. #pragma option push -b -a4 -Vx- -Ve- -w-inl -w-aus -w-sig
  4.  
  5. /***************************************************************************
  6.  *
  7.  * traits - Declarations for char_traits 
  8.  *
  9.  *
  10.  ***************************************************************************
  11.  *
  12.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  13.  * ALL RIGHTS RESERVED *
  14.  * The software and information contained herein are proprietary to, and
  15.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  16.  * intends to preserve as trade secrets such software and information.
  17.  * This software is furnished pursuant to a written license agreement and
  18.  * may be used, copied, transmitted, and stored only in accordance with
  19.  * the terms of such license and with the inclusion of the above copyright
  20.  * notice.  This software and information or any other copies thereof may
  21.  * not be provided or otherwise made available to any other person.
  22.  *
  23.  * Notwithstanding any other lease or license that may pertain to, or
  24.  * accompany the delivery of, this computer software and information, the
  25.  * rights of the Government regarding its use, reproduction and disclosure
  26.  * are as set forth in Section 52.227-19 of the FARS Computer
  27.  * Software-Restricted Rights clause.
  28.  * 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  31.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  32.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  33.  * P.O. Box 2328, Corvallis, Oregon 97339.
  34.  *
  35.  * This computer software and information is distributed with "restricted
  36.  * rights."  Use, duplication or disclosure is subject to restrictions as
  37.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  38.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  39.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  40.  * then the "Alternate III" clause applies.
  41.  *
  42.  **************************************************************************/
  43.  
  44. #include <string.h>
  45. #include <stdio.h>
  46.  
  47. //
  48. // Temporarily turn off the warnings under the Borland compiler that
  49. // say 'Functions containing ... cannot be inlined'
  50. //
  51. #if defined(__BORLANDC__)
  52. #pragma option -w-inl
  53. #endif
  54.  
  55. /*
  56.  * this are all necessary for the "traits" class
  57.  */
  58. #ifdef _RW_STD_IOSTREAM
  59. #include <rw/iotraits>
  60. #endif
  61.  
  62. #ifndef _RWSTD_NO_NAMESPACE
  63. namespace std {
  64. #endif
  65.  
  66.  
  67. typedef long         INT_T;
  68.  
  69.  
  70. /*
  71.  *     STRUCT CHAR_TRAITS
  72.  */
  73.  
  74. template<class charT>
  75. struct _RWSTDExportTemplate char_traits
  76. {
  77.  
  78.         typedef charT                     char_type;
  79.         typedef INT_T                     int_type;
  80.  
  81. #ifdef _RW_STD_IOSTREAM
  82.         typedef mbstate_t                 state_type;
  83.         typedef fpos<state_type>         pos_type;
  84.         typedef wstreamoff               off_type;
  85. #endif /* _RW_STD_IOSTREAM */
  86.  
  87.         static void assign (char_type& c1, const char_type& c2)   
  88.         { c1 = c2;}
  89.  
  90.       static char_type to_char_type(const int_type& c)
  91.       { return c; }
  92.  
  93.     static int_type to_int_type(const char_type& c)
  94.       { return c; }
  95.  
  96.     static bool     eq(const char_type& c1,const char_type& c2)
  97.       { return (c1 == c2); }
  98.  
  99.         static bool lt (const char_type& c1, const char_type& c2) 
  100.         { return c1 < c2;}
  101.  
  102.         static int compare (const char_type* s1, const char_type* s2, size_t n)
  103.         {
  104.           size_t i=0;
  105.  
  106.           while ( i < n )
  107.            { 
  108.              if ( !eq( s1[i], s2[i] ) )
  109.               {
  110.                  if ( lt( s1[i], s2[i]) )
  111.                   return -1;
  112.                  else
  113.                   return 1;               
  114.                }
  115.              i++;
  116.            }
  117.          return 0;
  118.         }
  119.     
  120.       static bool     eq_int_type(const int_type& c1,const int_type& c2)
  121.       { return (c1 == c2); }
  122.  
  123.       static int_type             eof()
  124.       { return EOF; }
  125.  
  126.       static int_type             not_eof(const int_type& c)
  127.       {
  128.             if(c == EOF)
  129.                  return 0;
  130.             else
  131.                  return c;
  132.       }
  133.  
  134.       static size_t               length(const char_type *s)
  135.       {
  136.           size_t l = 0;
  137.           while ( !eq(*s++, char_type(0) ) )  ++l;
  138.           return l;
  139.       }
  140.  
  141.         static const char_type* 
  142.         find (const char_type* s, int n, const char_type& a)
  143.         {
  144.           while (n-- > 0 && *s != a) 
  145.             ++s;
  146.           return  n >= 0 ? s : 0;
  147.         }
  148.  
  149.       static char_type    *copy(char_type *dst,const char_type *src, size_t n)
  150.       {
  151.           return (char_type*) memcpy((char *)dst,(char *)src,
  152.                                      n*sizeof(char_type));
  153.       }
  154.  
  155.         static char_type* move (char_type* s1, const char_type* s2, size_t n)
  156.         {
  157.           char_type * s = s1;
  158.           if (s1 < s2)
  159.            copy(s1, s2, n);
  160.           else if (s1 > s2)
  161.           {
  162.            s1 += n;
  163.            s2 += n;
  164.            for(size_t i = 0; i < n; ++i) assign(*--s1, *--s2);
  165.           }
  166.           return s;
  167.          }
  168.  
  169.         static char_type* 
  170.         assign (char_type* s, size_t n, const char_type& a)
  171.         {
  172.           char_type* tmp = s;
  173.           while (n-- > 0) 
  174.             *tmp++ = a;
  175.           return s;
  176.         }
  177.  
  178.   };
  179.  
  180.  
  181. /*
  182.  *
  183.  *     STRUCT CHAR_TRAITS SPECIALIZED FOR CHAR
  184.  *
  185.  */
  186.  
  187. _RWSTD_TEMPLATE
  188. struct _RWSTDExport char_traits<char> 
  189. {
  190.  
  191.       typedef char                      char_type;
  192.       typedef int                       int_type;
  193.        
  194. #ifdef _RW_STD_IOSTREAM
  195.         typedef streamoff                 off_type; 
  196.       typedef streampos                 pos_type;
  197.       typedef mbstate_t                 state_type;
  198. #endif /* _RW_STD_IOSTREAM */
  199.  
  200.         static void assign (char_type& c1, const char_type& c2)   
  201.          { c1 = c2; }
  202.  
  203.     static char_type         to_char_type(const int_type& c)
  204.      { return (char)c; }
  205.  
  206.       static int_type          to_int_type(const char_type& c)
  207.      { return (unsigned char)c; }
  208.  
  209.     static bool              eq(const char_type& c1,const char_type& c2)
  210.         { return (c1 == c2); }
  211.  
  212.         static bool lt (const char_type& c1, const char_type& c2) 
  213.         { return c1 < c2; }
  214.  
  215.         static int compare (const char_type* s1, const char_type* s2, size_t n)
  216.         { return memcmp(s1, s2, n); }
  217.  
  218.        static const char_type* 
  219.        find (const char_type* s, int n, const char_type& a)
  220.        { return (char_type*) memchr(s,a,n); }
  221.  
  222.       static bool         eq_int_type(const int_type& c1,const int_type& c2)
  223.      { return (c1 == c2); }
  224.  
  225.       static int_type          eof()
  226.         { return EOF; }
  227.  
  228.       static int_type          not_eof(const int_type& c)
  229.      {
  230.            if ( c == EOF )
  231.             return 0;
  232.            else
  233.             return c;
  234.      }
  235.               
  236.       static size_t            length(const char_type *s)
  237.      { return strlen(s); }
  238.  
  239.       static char_type  *copy(char_type *dst,const char_type *src, size_t n)
  240.         { return (char_type *) memcpy(dst, src, n); }
  241.  
  242.  
  243.         static char_type * move (char_type* s1, const char_type* s2, size_t n)
  244.         {
  245.           #ifndef _RWSTD_NO_MEMMOVE
  246.            return (char_type *) memmove(s1, s2, n);
  247.           #else
  248.            char_type * s = s1;
  249.            if (s1 < s2)
  250.             copy(s1, s2, n);
  251.            else if (s1 > s2)
  252.            {
  253.             s1 += n;
  254.             s2 += n;
  255.             for(size_t i = 0; i < n; ++i) assign(*--s1, *--s2);
  256.            }
  257.           return s;
  258.           #endif
  259.         }
  260.  
  261.         static char_type*
  262.         assign (char_type* s, size_t n, const char_type& a)
  263.         {
  264.           memset(s,a,n);
  265.           return s;
  266.         }
  267.  
  268.  };
  269.  
  270.  
  271. /*
  272.  *
  273.  *     STRUCT CHAR_TRAITS SPECIALIZED FOR WCHAR_T
  274.  *
  275.  */
  276.  
  277.  
  278. #ifndef _RWSTD_NO_WIDE_CHAR
  279.  
  280. _RWSTD_TEMPLATE
  281. struct _RWSTDExport char_traits<wchar_t> 
  282. {
  283.       typedef wchar_t                   char_type;
  284.  
  285. #ifndef _RWSTD_NO_WINT_TYPE
  286.       typedef wint_t                    int_type;
  287. #else
  288.       typedef long                      int_type;
  289. #endif
  290.  
  291. #ifndef WEOF
  292. #define WEOF (-1)
  293. #endif
  294.  
  295. #ifdef _RW_STD_IOSTREAM
  296.         typedef wstreamoff                off_type;
  297.       typedef wstreampos                pos_type;
  298.       typedef mbstate_t                 state_type;
  299. #endif /* _RW_STD_IOSTREAM */        
  300.  
  301.         static void assign (char_type& c1, const char_type& c2)   
  302.         { c1 = c2;}
  303.  
  304.       static char_type to_char_type(const int_type& c)
  305.       { return c; }
  306.  
  307.     static int_type to_int_type(const char_type& c)
  308.       { return c; }
  309.  
  310.     static bool     eq(const char_type& c1,const char_type& c2)
  311.       { return (c1 == c2); }
  312.  
  313.         static bool lt (const char_type& c1, const char_type& c2) 
  314.         { return c1 < c2;}
  315.  
  316.         static int compare (const char_type* s1, const char_type* s2, size_t n)
  317.         {
  318.          #ifndef _RWSTD_NO_WSTR
  319.           return memcmp(s1, s2, n*sizeof(char_type));
  320.          #else
  321.           size_t i=0;
  322.  
  323.           while ( i < n )
  324.            { 
  325.              if ( !eq( s1[i], s2[i] ) )
  326.               {
  327.                  if ( lt( s1[i], s2[i]) )
  328.                   return -1;
  329.                  else
  330.                   return 1;               
  331.                }
  332.              i++;
  333.            }
  334.          return 0;
  335.          #endif
  336.         }
  337.  
  338.       static bool     eq_int_type(const int_type& c1,const int_type& c2)
  339.       { return (c1 == c2); }
  340.  
  341.       static int_type             eof()
  342.       { return WEOF; }
  343.  
  344.       static int_type             not_eof(const int_type& c)
  345.       {
  346.             if(c == WEOF)
  347.                  return 0;
  348.             else
  349.                  return c;
  350.       }
  351.  
  352.       static size_t               length(const char_type *s)
  353.       {
  354.     #ifndef _RWSTD_NO_WSTR   
  355.             return wcslen(s);
  356.     #else
  357.              size_t l = 0;
  358.              while ( !eq(*s++, char_type(0) ) )  ++l;
  359.              return l;
  360.     #endif
  361.       }
  362.  
  363.         static const char_type* 
  364.         find (const char_type* s, int n, const char_type& a)
  365.         {
  366.         #ifndef _RWSTD_NO_WSTR 
  367.            return (char_type*) wmemchr(s,a,n);
  368.           #else
  369.           while (n-- > 0 && *s != a) 
  370.             ++s;
  371.           return  n >= 0 ? s : 0;
  372.           #endif
  373.         }
  374.  
  375.       static char_type    *copy(char_type *dst,const char_type *src, size_t n)
  376.       {
  377.               return (char_type*) memcpy((char *)dst,(char *)src,
  378.                                            n*sizeof(char_type));
  379.       }
  380.  
  381.         static char_type * move (char_type* s1, const char_type* s2, size_t n)
  382.         {
  383.           #ifndef _RWSTD_NO_MEMMOVE
  384.            return (char_type *) memmove(s1, s2, n*sizeof(char_type));
  385.           #else
  386.            char_type * s = s1;
  387.            if (s1 < s2)
  388.             copy(s1, s2, n);
  389.            else if (s1 > s2)
  390.            {
  391.             s1 += n;
  392.             s2 += n;
  393.             for(size_t i = 0; i < n; ++i) assign(*--s1, *--s2);
  394.            }
  395.           return s;
  396.           #endif
  397.         }
  398.  
  399.  
  400.         static char_type* 
  401.         assign (char_type* s, size_t n, const char_type& a)
  402.         {
  403.         #ifndef _RWSTD_NO_WSTR 
  404.           wmemset(s,a,n);
  405.           return s;
  406.         #else
  407.           char_type* tmp = s;
  408.           while (n-- > 0) 
  409.             *tmp++ = a;
  410.           return s;
  411.         #endif
  412.         }
  413.  
  414.  };
  415.  
  416. #endif
  417.  
  418.  
  419. #ifndef _RWSTD_NO_NAMESPACE
  420. } namespace __rwstd {
  421. using namespace std;
  422. #endif
  423. //
  424. // Implementation traits class, rw_traits, provides specialized
  425. // algorithms to speed up find operations on char and wchar_t strings
  426. //
  427.  
  428. template <class charT, class traits>
  429. struct _RWSTDExportTemplate rw_traits
  430. {
  431.   static const charT* find(const charT* s, const charT* v)
  432.   { 
  433.     size_t slen = traits::length(s);
  434.     size_t vlen = traits::length(v);
  435.     for (size_t xpos = 0; (xpos + vlen) <= slen ; xpos++)
  436.     {
  437.         bool found = true;
  438.         for (size_t i = 0; i < vlen ; i++)
  439.         {
  440.             if (!traits::eq(s[xpos + i], v[i]))
  441.             {
  442.                 found = false;
  443.                 break;
  444.             }
  445.         }
  446.         if (found)
  447.             return &s[xpos];
  448.     }
  449.  
  450.     return NULL;
  451.   }
  452.  
  453.   static const charT* rfind(const charT* s, charT v, size_t pos)
  454.   {
  455.     const charT* p = s + pos;
  456.     while (p >= s)
  457.     {
  458.       if (traits::eq(*p,v))
  459.         return p;
  460.       p--;
  461.     }
  462.     return NULL;       
  463.   }
  464.  
  465.   static size_t find_first_of(const charT* s, const charT* v)
  466.   {
  467.     const charT* p = s;
  468.     for (; *p; p++)
  469.     {
  470.       for (const charT* q = v; *q; q++)
  471.         if (traits::eq(*p, *q))
  472.            return p-s;
  473.     }
  474.     return  p-s;
  475.   }
  476.  
  477.   static size_t find_first_not_of(const charT* s, const charT* v)
  478.   {
  479.     const charT* p = s;
  480.     for (; *p; p++)
  481.     {
  482.       for (const charT* q = v; *q; q++)
  483.         if (!traits::eq(*p, *q))
  484.            return p-s;
  485.     }
  486.     return  p-s;
  487.   }
  488. };
  489.  
  490. _RWSTD_TEMPLATE
  491. struct _RWSTDExport rw_traits<char,char_traits<char> >
  492. {
  493.   static const char* find(const char* s, const char* v)
  494.   { return strstr(s,v); }
  495.   static const char* rfind(const char* s, char v, size_t pos)
  496.   { 
  497.     const char* p = s + pos;
  498.     while (p >= s)
  499.     {
  500.       if (char_traits<char>::eq(*p,v))
  501.         return p;
  502.       p--; 
  503.     }
  504.     return NULL;       
  505.   }
  506.   static size_t find_first_of(const char* s, const char* v)
  507.   { return strcspn(s,v); }
  508.   static size_t find_first_not_of(const char* s, const char* v)
  509.   { return strspn(s,v); }
  510. };
  511.  
  512. #ifndef _RWSTD_NO_WSTR
  513. _RWSTD_TEMPLATE
  514. struct _RWSTDExport rw_traits<wchar_t,char_traits<wchar_t> >
  515. {
  516.   static const wchar_t* find(const wchar_t* s, const wchar_t* v)
  517.   { return wcsstr(s,v); }
  518.   static const wchar_t* rfind(const wchar_t* s, wchar_t v, size_t pos)
  519.   { 
  520.     const wchar_t* p = s + pos;
  521.     while (p >= s)
  522.     {
  523.       if (char_traits<wchar_t>::eq(*p,v))
  524.         return p;
  525.       p--;
  526.     }
  527.     return NULL;       
  528.   }
  529.   static size_t find_first_of(const wchar_t* s, const wchar_t* v)
  530.   { return wcscspn(s,v); }
  531.   static size_t find_first_not_of(const wchar_t* s, const wchar_t* v)
  532.   { return wcsspn(s,v); }
  533. };
  534. #endif // _RWSTD_NO_WSTR
  535.  
  536. #ifndef _RWSTD_NO_NAMESPACE
  537. }
  538. #endif
  539.  
  540. #pragma option pop
  541. #endif // __RW_TRAITS
  542.